<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div style="color: #387bfc;" class="left">消息中心</div>
        <div class="right">
          <div class="HeaderOperation_main">
            <a
              class="button_item"
              @click.stop="setRead"
            >
              <div class="button_item_padding">
                <svg-icon icon-class="标为已读" />
                标为已读
              </div>
            </a>
          </div>
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          style="width: 99.9%"
          row-key="id"
          :cell-class-name="cellClassName"
          :cell-style="cellStyle"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['device:baseStandingBook:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, remove, messageRead, messageEmpty } from '@/api/system/message'
import { mapGetters } from 'vuex'
import { getDetails } from '@/api/saleManage/saleReview'
import ShowDetail from '@/views/saleManage/contractReview/ShowDetail'
import ShowDetailCompany from '@/views/system/notice/showDetail.vue'
import ShowDetailSystem from '@/views/superManage/reportForm/superNotice/showDetail.vue'
export default {
  name: 'Index',
  computed: {
    ...mapGetters([
      'userId', 'userName', 'avatar', 'logo', 'notificationTag', 'notificationNum', 'companyName'
    ])
  },
  watch: {
    notificationTag() {
      this.getList()
    }
  },
  data() {
    return {
      loading: true,
      pageId: '消息通知', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      typeData: {
        status: 1
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态' },
        { prop: 'title', label: '消息标题' },
        { prop: 'content', label: '消息内容' },
        { prop: 'typeName', label: '消息类型' },
        { prop: 'userName', label: '发送人员' },
        { prop: 'sendTime', label: '发送时间' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'title', label: '消息标题', type: 'input' },
        { prop: 'type', label: '消息类型', type: 'select', optionsName: 'statusOptionsType' },
        { prop: 'status', label: '状态', type: 'select', optionsName: 'statusOptions', isShow: false },
        { prop: 'userName', label: '发送人员', type: 'input' }
      ],
      statusOptionsType: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: 1, label: '消息' },
        { value: 2, label: '公告' },
        { value: 3, label: '待办' }
      ],
      statusOptions: [
        { value: undefined, label: '全部' },
        { value: 1, label: '已读' },
        { value: 0, label: '未读' }
      ],
      operationData: [ // 右上角的操作按钮
        // { label: '新增', perm: 'device:baseStandingBook:add' },
        // { label: '导入', perm: 'device:baseStandingBook:import' },
        { label: '批量操作', perm: '' },
        { label: '批量删除', perm: 'device:baseStandingBook:delete' },
        // { label: '批量导出', perm: 'device:baseStandingBook:export' },
        // { label: '打印', perm: '' },
        { label: '页面配置', perm: '' }
      ],

      equipmentInfoOptions: [],
      showKey: {
        type: 'column_two',
        keys: [
          {
            name: '基本信息',
            columns: [
              { type: 'form', name: '合同评审单', key: 'code' },
              { type: 'form', name: '制单人员', key: 'reviewName' },
              { type: 'form', name: '制单日期', key: 'reviewTime' },
              { type: 'form', name: '客户特殊要求', key: 'specialRequire', length: 'double' },
              { type: 'form', name: '备注', key: 'remark', length: 'double' },
              {
                type: 'custom_table', name: '', key: 'details'
              }
            ]
          },
          {
            name: '需求清单',
            columns: [
              {
                type: 'table', name: '需求清单', key: 'needList', columns: [
                  { type: 'column', name: '物料编码', key: 'materialCode' },
                  { type: 'column', name: '物料名称', key: 'materialName' },
                  { type: 'column', name: '规格型号', key: 'materialModel' },
                  { type: 'column', name: '物料分类', key: 'materialTypeName' },
                  { type: 'column', name: '物料属性', key: 'materialDescriptionName' },
                  { type: 'column', name: '物料单位', key: 'materialUnitName' },
                  { type: 'column', name: '库存数量', key: 'stockNum' },
                  { type: 'column', name: '需求数量', key: 'needNumber' },
                  { type: 'column', name: '替代料', key: 'replaceMaterialCodeName' }
                ]
              }
            ]
          },
          {
            name: '审批进程',
            columns: [
              { type: 'review', name: '审批进程', key: 'taskList' }
            ]
          }
        ]
      }
    }
  },
  methods: {
    cellClassName({ row, column, rowIndex, columnIndex }) {
      // if (column.property == 'statusName') {
      //   switch (row.status) {
      //     case 0:
      //       return 'statusCell statusCell0'
      //     case 1:
      //       return 'statusCell statusCell1'
      //     default:
      //       return ''
      //   }
      // }
    },
    cellStyle({ row, column }) {
      if (column.property == 'content' && row.title != '即将到期' && row.title != '系统通知') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      console.log(row)
      switch (column.property) {
        case 'content':
          if (row.type == 1 && row.title != '即将到期' && row.title != '系统通知') {
            this.oneMessageRead(row)
          } else if (row.type == 2 && row.title != '即将到期' && row.title != '系统通知') {
            this.oneNoticeRead(row)
          } else if (row.type == 3 && row.title != '即将到期' && row.title != '系统通知') {
            this.oneWaitRead(row)
          }
          break
      }
    },
    oneMessageRead(item) {
      messageEmpty(item.id).then(res => {
        this.getList()
        if (item.menuPermission == '0') {
          this.$message.warning('您没有当前页面的权限,请联系管理员')
        } else if (item.menuName == '合同评审单') {
          // 如果是 合同评审 ， 直接打开评审页面
          getDetails(item.contentId).then(res => {
            this.$layer.iframe({// 弹框
              content: {
                content: ShowDetail, // 传递的组件对象
                parent: this, // 当前的vue对象
                data: {
                  showData: res.data,
                  showKey: this.showKey
                }// props
              },
              area: ['880px', '700px'],
              title: '查看合同评审单', // 标题
              shadeClose: false // 点击遮罩是否关闭
            })
          })
        } else {
          this.$store.dispatch('notification/changeId', item.contentId)
          this.$router.push('/' + item.menuComponent)
          this.$store.dispatch('notification/changeDataStatus', item.menuStatus)
        }
      })
    },
    oneNoticeRead(item) {
      messageRead(item.id).then(res => {
        this.getList()
        if (item.menuPermission == '0' && !item.title == '系统公告' && !item.title == '企业公告') {
          this.$message.warning('您没有当前页面的权限,请联系管理员')
        } else {
          if (item.title == '系统公告') {
            this.$layer.iframe({// 弹框
              content: {
                content: ShowDetailSystem, // 传递的组件对象
                parent: this, // 当前的vue对象
                data: {
                  id: item.contentId
                }// props
              },
              area: ['880px', '820px'],
              title: '公告详情', // 标题
              shadeClose: false // 点击遮罩是否关闭
            })
          } else {
            this.$layer.iframe({// 弹框
              content: {
                content: ShowDetailCompany, // 传递的组件对象
                parent: this, // 当前的vue对象
                data: {
                  id: item.contentId
                }// props
              },
              area: ['880px', '820px'],
              title: '公告详情', // 标题
              shadeClose: false // 点击遮罩是否关闭
            })
          }
          // this.$store.dispatch('notification/changeId', item.contentId)
          // this.$router.push('/' + item.menuComponent)
          // this.$store.dispatch('notification/changeDataStatus', item.menuStatus)
        }
      })
    },
    oneWaitRead(item) {
      messageRead(item.id).then(res => {
        this.getList()
        if (item.menuPermission == '0') {
          this.$message.warning('您没有当前页面的权限,请联系管理员')
        } else if (item.menuName == '合同评审单') {
          // 如果是 合同评审 ， 直接打开评审页面
          getDetails(item.contentId).then(res => {
            this.$layer.iframe({// 弹框
              content: {
                content: ShowDetail, // 传递的组件对象
                parent: this, // 当前的vue对象
                data: {
                  showData: res.data,
                  showKey: this.showKey
                }// props
              },
              area: ['880px', '700px'],
              title: '查看合同评审单', // 标题
              shadeClose: false // 点击遮罩是否关闭
            })
          })
        } else {
          this.$store.dispatch('notification/changeId', item.contentId)
          this.$store.state.order.isEdit = true
          this.$router.push('/' + item.menuComponent)
          this.$store.dispatch('notification/changeDataStatus', item.menuStatus)
        }
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getExportData(data) {
      return list(data)
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    },
    setRead() {
      const _this = this
      if (this.selectionsAll.length == 0) {
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning'
        })
      } else {
        this.$confirm('确定要标为已读吗', '操作确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
          type: 'warning'
        }).then(function() {
          const ids = _this.selectionsAll.map(item => {
            return item.id
          })
          messageRead(ids.join(',')).then(res => {
            _this.$message({
              message: res.msg,
              type: 'success'
            })
            _this.selectionsAll = []
            _this.getList()
          })
        }).catch(() => {})
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .statusCell{
  height: 44px;
  .cell{
    display: inline;
    padding-left: 5px;
  }
}
/deep/ .statusCell::before{
  content: "";
  width: 11px;height: 10px;border-radius: 50%;
  margin-left: 10px;
  display: inline-block;
}
/deep/ .statusCell0::before{
  background-color: #FF9900;
}
/deep/ .statusCell1::before{
  background-color: #19BE6B;
}
</style>
